﻿using UnityEngine;
using System.Collections;

public class FPSFixer : MonoBehaviour
{

    private int normal;
    private float lastInterval;

    public int fps;
    public UILabel lable;
    public int targetFps;


    public int InitFrameRate = 30;//初始化时的帧率
    public int MaxFrameRate = 30;//最大帧率
    public int MinFrameRate = 5;//最低帧率
    public int IncreaseDelta = 1;//提升帧率时的 实际帧率与目标帧率的差
    public int DecreaseDelta = 3;//降低帧率时的 实际帧率与目标帧率的差
    public int UpdateSeconds = 10;//更新目标帧率的时间间隔

    private bool preloadType = false;

    // Use this for initialization
    void Start()
    {
        lastInterval = Time.realtimeSinceStartup;
        Application.targetFrameRate = InitFrameRate;
        if (lable == null)
            lable = GetComponent<UILabel>();
    }

    // Update is called once per frame
    void Update()
    {
        if(AssetLoad.AssetBundleManager.Instance.IsLoadingScene != preloadType)
        {
            preloadType = AssetLoad.AssetBundleManager.Instance.IsLoadingScene;
            if(preloadType)
            {
                Application.targetFrameRate = 60;
            }
            else
            {
                Application.targetFrameRate = InitFrameRate;
            }
        }
        if(preloadType)
        {
            return;
        }
        if (Time.deltaTime > 1f)
        {
            lastInterval += Time.deltaTime;
            return;
        }

        ++normal;

        float delta = (Time.realtimeSinceStartup - lastInterval) - (float)normal / (float)Application.targetFrameRate;
        if (delta < -0.001f)
        {
            System.Threading.Thread.Sleep((int)Mathf.Floor(-delta * 1000));
        }

        float timeNow = Time.realtimeSinceStartup;
        if (timeNow > lastInterval + 1f)
        {
            int n = (int)((float)normal / (timeNow - lastInterval));
            lastInterval = timeNow;
            fps = n;
            normal = 0;

#if UNITY_EDITOR

#else
            Refresh();
#endif
        }

    }

    private int refreshTimes = 0;
    private float sumFPS = 0;
    void Refresh()
    {
        refreshTimes++;
        sumFPS += fps;

        

        if (refreshTimes >= UpdateSeconds)
        {
            int maxfps = Mathf.Min(RenderManager.MaxFPS, MaxFrameRate);
            targetFps = Application.targetFrameRate;

            float averageFPS = sumFPS / (float)refreshTimes;

            if (averageFPS >= targetFps - IncreaseDelta)
                targetFps++;

            if (averageFPS < targetFps - DecreaseDelta)
                targetFps--;

            if(targetFps>maxfps)
                targetFps = maxfps;

            if(targetFps<MinFrameRate)
                targetFps = MinFrameRate;

            if(targetFps!=Application.targetFrameRate)
            {
                Application.targetFrameRate = targetFps;
            }
            refreshTimes = 0;
            sumFPS = 0;

            if (lable != null)
            {
                lable.text = "averageFPS:" + averageFPS + " targetFPS:" + Application.targetFrameRate;
            }
        }
    }

}
